#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: tapclient_connector.py 
@version:
@time: 2022/01/14 
"""
import jpype

from TapClientConnector.connector import *
from TapClientCore.T01_init_conf.path_conf import PathConf


class TapClientConnector:
    tapclient_connector = None

    def __init__(self, db_info):
        self.database_type = db_info.get('database_type').lower()
        jpype.addClassPath(PathConf.JAR_GBASE_8S_PATH)
        jpype.addClassPath(PathConf.JAR_DM_PATH)
        jpype.addClassPath(PathConf.JAR_KINGBASE_R3_PATH)
        try:
            jpype.startJVM()
            self.jvm = True
        except OSError:
            logger.info('jvm already start')
            pass

        self.init_datasource(db_info)

    def init_datasource(self, db_info):
        try:
            if self.database_type in [
                'mysql', 'mariadb', 'mysql-pxc',
                'tidb', 'kundb', 'adb_mysql',
                'doris', 'oceanbase', 'gbase-8a'
            ]:
                self.tapclient_connector = TAPMysqlConnector(db_info)
            elif self.database_type == 'gbase-8s':
                self.tapclient_connector = TAPGbase8SConnector(db_info)
            elif self.database_type == 'mongodb':
                self.tapclient_connector = TAPMongoDBConnector(db_info)
            elif self.database_type == 'oracle':
                self.tapclient_connector = TAPOracleConnector(db_info)
            elif self.database_type == 'db2':
                self.tapclient_connector = TAPDB2Connector(db_info)
            elif self.database_type in ['postgres', 'postgresql', 'greenplum', 'adb_postgres']:
                self.tapclient_connector = TAPPostgresConnector(db_info)
            elif self.database_type in ['gaussdb200']:
                self.tapclient_connector = TAPGaussDBConnector(db_info)
            elif self.database_type in ['sqlserver', 'mssql', 'sql server']:
                self.tapclient_connector = TAPSqlserverConnector(db_info)
            elif self.database_type == 'dameng':
                self.tapclient_connector = TAPDMConnector(db_info)
            elif self.database_type == 'kingbasees-r3':
                self.tapclient_connector = TAPKingBaseR3Connector(db_info)
            elif self.database_type == 'kingbasees-r6':
                self.tapclient_connector = TAPKingBaseR6Connector(db_info)
            elif self.database_type == 'elasticsearch':
                self.tapclient_connector = TAPESConnector(db_info)
            elif self.database_type == 'redis':
                self.tapclient_connector = TAPRedisConnector(db_info)
            elif self.database_type == 'kafka':
                self.tapclient_connector = TAPKafkaConnector(db_info)
            elif self.database_type == 'mq':
                if db_info.get('mqType') == '0':
                    self.tapclient_connector = TapActiveMQConnector(db_info)
                elif db_info.get('mqType') == '1':
                    self.tapclient_connector = TapRabbitMQConnector(db_info)
                elif db_info.get('mqType') == '2':
                    self.tapclient_connector = TapRocketMQConnector(db_info)
                else:
                    logger.fatal('not support')
            elif self.database_type == 'activemq':
                self.tapclient_connector = TapActiveMQConnector(db_info)
            elif self.database_type == 'rabbitmq':
                self.tapclient_connector = TapRabbitMQConnector(db_info)
            elif self.database_type == 'rocketmq':
                self.tapclient_connector = TapRocketMQConnector(db_info)
            elif self.database_type in ['hana', 'sybase ase']:
                self.tapclient_connector = TAPHanaConnector(db_info)
            elif self.database_type == 'clickhouse':
                self.tapclient_connector = TAPClickHouseConnector(db_info)
            elif self.database_type in ['hive', 'hive1']:
                self.tapclient_connector = TAPHiveConnector(db_info)
            elif self.database_type == 'hbase':
                self.tapclient_connector = TAPHbaseConnector(db_info)
            else:
                raise NotSupport
            logger.info(f'connector to {db_info.get("name")} success')
        except NotSupport:
            logger.fatal('not support')
        # except Exception as e:
        #     logger.fatal(f'connector to {db_info.get("name")} failed')
        #     raise ConnectorInitFaild


if __name__ == '__main__':
    from TapDataSourceInfo.db_info_pdk import *

    t1 = TapClientConnector(Gbase8a.AUTO_Gbase8a)
    t2 = TapClientConnector(DM.AUTO_DM_SOURCE)
    t3 = TapClientConnector(KingBaseES.AUTO_KingBaseR6_SOURCE)
    t4 = TapClientConnector(KingBaseR3.AUTO_KingBaseR3_SOURCE)
    print(t1.tapclient_connector.show_dbs())
    print(t2.tapclient_connector.show_dbs())
    print(t3.tapclient_connector.show_dbs())
    print(t4.tapclient_connector.show_dbs())
